//
//  CJMorePopverView.swift
//  CJBookReader
//
//  Created by lidi on 2017/11/29.
//  Copyright © 2017年 lidi. All rights reserved.
//

import UIKit

//更多弹框
class CJMorePopverView: UIView ,UITableViewDataSource , UITableViewDelegate {

    private var searchHandle:(()->Void)? //全文检索
    private var markHandle:(()->Void)? //书签
    private var shareHandle:(()->Void)? //分享
    private var rewardHandle:(()->Void)? //打赏
    private var hasMark = false
    private var hasReward = false
    
    private lazy var infoTableView = UITableView(frame: CGRect.zero, style: .plain)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let maskView = UIView()
        self.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        maskView.isUserInteractionEnabled = true
        let closeTap = UITapGestureRecognizer(target: self, action: #selector(self.close))
        maskView.addGestureRecognizer(closeTap)
        
        self.infoTableView.dataSource = self
        self.infoTableView.delegate = self
        self.infoTableView.backgroundColor = UIColor.readMenu.bg
        self.addSubview(self.infoTableView)
        self.infoTableView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-5.0)
            make.top.equalToSuperview().offset(CGFloat.navgationBarHeight)
            make.width.equalTo(115.0)
            make.height.equalTo(44.0 * 1)
        }
        self.infoTableView.register(Cell.self, forCellReuseIdentifier: "Cell")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func show(hasMark:Bool,hasReward:Bool,
              searchHandle:(()->Void)?,
              markHandle:(()->Void)?,
              shareHandle:(()->Void)?,
              rewardHandle:(()->Void)?) {
        self.hasMark = hasMark
        self.hasReward = hasReward
        self.searchHandle = searchHandle
        self.markHandle = markHandle
        self.shareHandle = shareHandle
        self.rewardHandle = rewardHandle
        self.infoTableView.reloadData()
        
        let container = UIApplication.shared.keyWindow
        container?.addSubview(self)
        self.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.infoTableView.snp.updateConstraints { (make) in
            if self.hasReward {
                make.height.equalTo(44.0 * 2)
            } else {
                make.height.equalTo(44.0 * 1)
            }
        }
        
        self.infoTableView.alpha = 0
        self.layoutIfNeeded()
        
        UIView.animate(withDuration: 0.3) { [weak self] in
            self?.infoTableView.alpha = 1.0
        }
    }
    
    //MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.hasReward {
            return 2
        } else {
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
        
        var imageName = ""
        var title = ""
        
        switch indexPath.row {
//        case 0:
//            imageName = "more_search"
//            title = "全文检索"
        case 0:
            if self.hasMark {
                imageName = "more_mark_enabled"
                title = "移除书签"
            } else {
                imageName = "more_mark_normal"
                title = "添加书签"
            }
//        case 2:
//            imageName = "more_share"
//            title = "分享"
        case 1:
            imageName = "more_reward"
            title = "打赏"
        default: break
        }
        
        cell.refresh(imageName: imageName, title: title)
        return cell
    }
    
    //MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
//        if indexPath.row == 0 {
//            self.searchHandle?()
//        }
        
        if indexPath.row == 0 {
            self.markHandle?()
        }
        
//        if indexPath.row == 2 {
//            self.shareHandle?()
//        }
        
        if indexPath.row == 1 {
            self.rewardHandle?()
        }
        
        self.close()
    }
    
    @objc private func close() {
        UIView.animate(withDuration: 0.3, animations: { [weak self] in
            self?.infoTableView.alpha = 0
        }) {[weak self] (finish) in
            self?.removeFromSuperview()
        }
    }
    
    private class Cell:UITableViewCell {
        
        private lazy var imageViewIcon = UIImageView()
        private lazy var labelTitle = UILabel()
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.backgroundColor = UIColor.clear
            self.contentView.backgroundColor = UIColor.clear
            self.selectionStyle = .none
            
            self.imageViewIcon.contentMode = .center
            self.contentView.addSubview(self.imageViewIcon)
            self.imageViewIcon.snp.makeConstraints { (make) in
                make.left.top.bottom.equalToSuperview()
                make.width.equalTo(40.0)
            }
            
            self.labelTitle.font = UIFont.systemFont(ofSize: 13.0)
            self.labelTitle.textColor = UIColor.white
            self.contentView.addSubview(self.labelTitle)
            self.labelTitle.snp.makeConstraints { (make) in
                make.left.equalTo(self.imageViewIcon.snp.right)
                make.right.equalToSuperview()
                make.centerY.equalToSuperview()
            }
        }
        
        func refresh(imageName:String,title:String) {
            self.imageViewIcon.image = UIImage(named: imageName, in: Bundle(for: type(of: self)), compatibleWith: nil)
            self.labelTitle.text = title
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}
